<template>
  <section class="webview fixHeight">
    <el-scrollbar style="height:100%">
      <el-form ref="form" :model="detail" label-width="100px" class="form">
        <el-form-item label="户型名称">
          <el-input v-model="detail.typeName" />
        </el-form-item>
        <el-form-item label="户型标签">
          <el-input v-model="detail.label" />
        </el-form-item>
        <el-form-item label="销售状态">
          <el-select v-model="detail.salesStatus" placeholder="请选择">
            <el-option
              v-for="item in salesStatusOptions"
              :key="item.id"
              :label="item.title"
              :value="item.title"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="建筑面积">
          <el-input v-model="detail.area" />
        </el-form-item>
        <el-form-item label="选择楼盘">
          <el-select v-model="detail.residentialId" placeholder="请选择" @change="changeResidentialId">
            <el-option
              v-for="item in residentialIdOptions"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="朝向">
          <el-input v-model="detail.orientation" />
        </el-form-item>
        <el-form-item label="价格">
          <el-input v-model="detail.totalPrice" />
        </el-form-item>
        <el-form-item label="是否主力">
          <el-radio v-model="detail.isMain" :label="true">是</el-radio>
          <el-radio v-model="detail.isMain" :label="false">否</el-radio>
        </el-form-item>
        <el-form-item label="排序">
          <el-input v-model="detail.sort" />
        </el-form-item>
        <el-form-item label="户型图">
          <div class="picList">
            <div v-for="(item,index) in fileList" class="item">
              <img class="pic" :src="item" alt>
              <i class="el-icon-delete delIcon hand" @click="fileList.splice(index, 1);detail.apartmentPicture = ''" />
            </div>
          </div>
          <div v-if="fileList.length < 1" class="item itemNo">
            <el-upload
              :action="Is.sevenDomain"
              :multiple="false"
              name="file"
              :limit="1"
              :show-file-list="false"
              :before-upload="handleUpload"
              :on-success="(res,file)=>{if(res.code == 200){fileList.push(res.data);detail.apartmentPicture = fileList[0]}}"
            >
              <div slot="tip" class="el-upload__tip">只能上传jpg/png文件，只能上传1张，且不超过10M</div>
              <div class="el-upload el-upload--picture-card">
                <i slot="default" class="el-icon-plus" />
              </div>
            </el-upload>
          </div>
        </el-form-item>

        <el-form-item>
          <el-button type="primary" @click="submit">提交</el-button>
          <el-button @click="cancel">返回</el-button>
        </el-form-item>
      </el-form>
    </el-scrollbar>
  </section>
</template>
<script>
import { propertiesList, typeListSon, apartmentAdd, apartmentUpdate } from '@/api/api'
export default {
  data() {
    return {
      fileList: [],
      buildingTypeOptions: [],
      residentialIdOptions: [],
      salesStatusOptions: [],
      postIdOptions: [
        {
          value: 1,
          label: 'APP端'
        },
        {
          value: 2,
          label: 'PC端'
        }],
      detail: {
        'apartmentPicture': '',
        'apartmentType': '',
        'area': 0,
        'dataName': '',
        'id': '',
        'isMain': true,
        'label': '',
        'orientation': '',
        'residentialId': '',
        'salesStatus': '',
        'sort': 0,
        'totalPrice': 0,
        'typeName': ''
      }
    }
  },
  created() {
    if (this.$route.query.item) {
      this.detail = JSON.parse(this.$route.query.item)
      this.fileList[0] = this.detail.apartmentPicture
    }
    this.getMb()
  },
  methods: {
    getMb() {
      // 小区列表
      propertiesList({
        curPage: 1,
        pageSize: 999,
        name: ''
      }).then(data => {
        this.residentialIdOptions = data.data.records
      })
      // 建筑类型
      typeListSon('8e612c9d4a0dc5f7c3ef76df6e394091').then(({ data }) => {
        this.buildingTypeOptions = data
      })
      // 销售类型
      typeListSon('b85139395c06993f94e9fce2b96e9b43').then(({ data }) => {
        this.salesStatusOptions = data
      })
    },
    changeResidentialId(value) {
      const a = this.residentialIdOptions.filter(item => item.id == value)
      this.detail.dataName = a[0].name
    },
    handleRemove(index) {
      this.fileList.splice(index, 1)
    },
    // 上传图片之前的验证
    handleUpload() {
    },
    // 上传成功之后的回调
    handleSuccess(res) {
      if (res.code === 200) {
        this.fileList.push(res.data)
        this.detail.logo = this.fileList[0]
      }
    },
    // 添加/编辑
    submit() {
      // 验证不通过则不提交
      if (!this.vertify()) {
        return false
      }
      if (this.$route.query.item) {
        // 编辑
        apartmentUpdate(this.detail).then(data => {
          Is.tip('编辑成功', 2000, 'success')
          this.$router.back()
        })
      } else {
        // 新增
        apartmentAdd(this.detail).then(data => {
          Is.tip('添加成功', 2000, 'success')
          this.$router.back()
        })
      }
    },
    // 返回
    cancel() {
      this.$router.back()
    },
    vertify() {
      // if (Is.empty(this.detail.title)) {
      //   Is.tip('请输入标题')
      //   return false
      // }
      return true
    }
  }}
</script>
